package com.amkai.mobile.client.appointmentdetails;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.Toast;

import com.amkai.mobile.client.R;
import com.amkai.mobile.client.common.AEMobileConstants;
import com.amkai.mobile.client.common.SharedDataProvider;
import com.amkai.mobile.client.options.OptionsProvider;
import com.amkai.mobile.common.job.scheduler.MobileSchedulerJob;
import com.amkai.mobile.common.job.scheduler.MobileSchedulerJobResult;
import com.amkai.mobile.utils.manager.job.MobileServiceManager;

/**
 * 
 * @author lkovari
 *
 */
public class AppointmentDetail extends TabActivity {

	/**
	 * 
	 * @param currentContent
	 * @param moduleIntent
	 * @param beId
	 * @param passportId
	 * @param apptId
	 */
	private void passCommonInfo(Activity currentContent, Intent moduleIntent, Long beId, Long passportId, Long apptId) {
		Bundle b = new Bundle();
		b.putLong(AEMobileConstants.BUNDLE_CONTENT_TAG_DEPARTMENTID, beId);
		b.putLong(AEMobileConstants.BUNDLE_CONTENT_TAG_PASSPORTID, passportId);
		b.putLong(AEMobileConstants.BUNDLE_CONTENT_TAG_APPOINTMENTID, apptId);
		moduleIntent.putExtras(b);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return OptionsProvider.onCreateOptionsMenu(this, menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		return OptionsProvider.onOptionsItemSelected(this, item);
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.appointmentdetail);
        
        // capture ids	
		Bundle bundle = getIntent().getExtras();
        Long currentBEId = bundle.getLong(AEMobileConstants.BUNDLE_CONTENT_TAG_DEPARTMENTID); 
        Long currentPassId = bundle.getLong(AEMobileConstants.BUNDLE_CONTENT_TAG_PASSPORTID);
        Long apptId = bundle.getLong(AEMobileConstants.BUNDLE_CONTENT_TAG_APPOINTMENTID);	
        String providerAs = bundle.getString(AEMobileConstants.BUNDLE_CONTENT_TAG_PROVIDERAS);
        Object o = bundle.get(AEMobileConstants.BUNDLE_CONTENT_TAG_PROVIDERAS);
        Boolean isPatient = bundle.getBoolean(AEMobileConstants.BUNDLE_CONTENT_TAG_IS_PATIENT);
        
        SharedDataProvider sharedDataProvider = SharedDataProvider.getInstance();
        try {
			this.setTitle(AEMobileConstants.APP_TITLE  +" - " + sharedDataProvider.getBeNameById(this, currentBEId) + " [" + sharedDataProvider.getPassportName(this)+" as " + providerAs + "]");
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        
        // download appointmentTetails
        boolean isSCorSH = false;
		try {
			MobileSchedulerJobResult schedulerResult = MobileServiceManager.processMobileJob(this, MobileSchedulerJob.appointmentKindById(apptId));
			if (schedulerResult.getException() == null) {
				isSCorSH = schedulerResult.isSCorSHAppointment();
			}
			else {
				Toast msg = Toast.makeText(this, "Download appointment kind data failed!", Toast.LENGTH_SHORT);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
        
        
        /* TabHost will have Tabs */
        TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
        
        /* TabSpec used to create a new tab. 
         * By using TabSpec only we can able to setContent to the tab.
         * By using TabSpec setIndicator() we can set name to tab. */
        
        /* tid1 is firstTabSpec Id. Its used to access outside. */
       	Resources res = getResources();
       	TabSpec appointmentTabSpec = tabHost.newTabSpec("appointment");
        TabSpec patientTabSpec = tabHost.newTabSpec("patient");
        TabSpec caseTabSpec = tabHost.newTabSpec("case");

        /* TabSpec setIndicator() is used to set name for the tab. */
        /* TabSpec setContent() is used to set content for a particular tab. */
        Intent appointmentTabIntent = null;
        if (isSCorSH) {
        	appointmentTabIntent = new Intent(this, AppointmentTabSC.class);
        }	
        else {
        	appointmentTabIntent = new Intent(this, AppointmentTabPM.class);
        }	
        passCommonInfo(this, appointmentTabIntent, currentBEId, currentPassId, apptId);
//        appointmentTabSpec.setIndicator("Appointment").setContent(appointmentTabIntent);
       	appointmentTabSpec.setIndicator("Appointment", res.getDrawable(R.drawable.ic_tab_appt)).setContent(appointmentTabIntent);
        
        Intent patientTabIntent = new Intent(this, PatientTab.class);
        passCommonInfo(this, patientTabIntent, currentBEId, currentPassId, apptId);
//        patientTabSpec.setIndicator("Patient").setContent(patientTabIntent);
        String personpatient = "Patient";
        if (isPatient)
        	personpatient = "Patient";
        else
        	personpatient = "Person";
        patientTabSpec.setIndicator(personpatient, res.getDrawable(R.drawable.ic_tab_person)).setContent(patientTabIntent);
        
        Intent caseTabIntent = new Intent(this, CaseTab.class);
        passCommonInfo(this, caseTabIntent, currentBEId, currentPassId, apptId);
//        caseTabSpec.setIndicator("Case").setContent(caseTabIntent);
        caseTabSpec.setIndicator("Case", res.getDrawable(R.drawable.ic_tab_case)).setContent(caseTabIntent);
        
        
        /* Add tabSpec to the TabHost to display. */
        tabHost.addTab(appointmentTabSpec);
        tabHost.addTab(patientTabSpec);
        tabHost.addTab(caseTabSpec);
        
    }

}
